package com.ruoyi.web.controller.Goods;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Goods.domain.Goods;
import com.ruoyi.Goods.service.IGoodsService;
import com.ruoyi.GoodsAttach.domain.GoodsAttach;
import com.ruoyi.GoodsAttach.service.IGoodsAttachService;
import com.ruoyi.GoodsLx.domain.GoodsLx;
import com.ruoyi.GoodsLx.service.IGoodsLxService;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.APPConstants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商品 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-16
 */
@Controller
@RequestMapping("/Goods/goodsVerify")
public class GoodsVerifyController extends BaseController
{
    private String prefix = "Goods/goodsVerify";
	
	@Autowired
	private IGoodsService goodsService;
	
	@Autowired
	private IGoodsAttachService goodsAttachService;
	
	@Autowired
	private IGoodsLxService goodsLxService;
	
	@RequiresPermissions("Goods:goods:view")
	@GetMapping()
	public String goods(ModelMap mmap,GoodsLx goodsLx)
	{
		List<GoodsLx> goodsLxList=goodsLxService.selectGoodsLxList(goodsLx);
		mmap.put("goodsLxList", goodsLxList);
	    return prefix + "/goods";
	}
	
	/**
	 * 查询商品列表
	 */
	@RequiresPermissions("Goods:goods:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Goods goods)
	{
		goods.setValid(0);
		goods.setGoodsLayout(APPConstants.SHENHEZHONG);
		startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商品列表
	 */
	@RequiresPermissions("Goods:goods:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Goods goods)
    {
    	List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "goods");
    }
	
	/**
	 * 新增商品
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap,GoodsLx goodsLx,Goods goods)
	{
		int a =(int)((Math.random()*9+1)*10000000);
		String currentId=String.valueOf(a);
		goods.setGoodsYuliu2(currentId);
		List<GoodsLx> goodsLxList=goodsLxService.selectGoodsLxList(goodsLx);
		List<GoodsAttach> list=Collections.EMPTY_LIST;
		String previewListEdit=JSON.toJSON(list).toString();
	 	mmap.put("previewListEdit",previewListEdit);
		mmap.put("goodsLxList", goodsLxList);
		mmap.put("goods", goods);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品
	 */
	@RequiresPermissions("Goods:goods:add")
	@Log(title = "商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Goods goods) {
		GoodsAttach attach = new GoodsAttach();
		goodsService.insertGoods(goods);
		String attachGoodsId = goods.getGoodsYuliu2();
		int currentId = Integer.parseInt(attachGoodsId);
		attach.setGoodsId(currentId);
		List<GoodsAttach> GoodsAttachList = goodsAttachService
				.selectGoodsAttachList(attach);
		if (GoodsAttachList.size() > 0) {
			String firstImage = GoodsAttachList.get(0).getUrl();
			Goods example = new Goods();
			example.setGoodsYuliu2(attachGoodsId);
			List<Goods> goodsList = goodsService.selectGoodsMathList(example);
			if (goodsList.size() > 0) {
				Goods persist = goodsList.get(0);
				int realId = persist.getId();
				for (GoodsAttach forEachAttach : GoodsAttachList) {
					forEachAttach.setGoodsId(realId);
					goodsAttachService.updateGoodsAttach(forEachAttach);
				}
				persist.setGoodsYuliu2("0");
				persist.setGoodsTp(firstImage);
				
				goodsService.updateGoods(persist);
			}
		}
		return AjaxResult.success("添加商品成功");
	}

	/**
	 * 修改商品
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		GoodsAttach goodsAttach=new GoodsAttach();
   	 	goodsAttach.setGoodsId(id);
   	 	List<GoodsAttach> list=goodsAttachService.selectGoodsAttachList(goodsAttach);
   	 	String previewListEdit=JSON.toJSON(list).toString();
	 	mmap.put("previewListEdit",previewListEdit);
		Goods goods = goodsService.selectGoodsById(id);
		mmap.put("goods", goods);
		GoodsLx goodsLx=new GoodsLx();
		List<GoodsLx> goodsLxList=goodsLxService.selectGoodsLxList(goodsLx);
		mmap.put("goodsLxList", goodsLxList);		
	    return prefix + "/edit";
	}
	
	/**
	 * 商品图片上传
	 */
	@PostMapping("/upload/{goods_id}")
	@ResponseBody
	public AjaxResult upload(@RequestParam("file") MultipartFile[] files,
			@PathVariable("goods_id") String goods_id) throws IOException {
		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];
			if (file.isEmpty()) {
				return AjaxResult.error();
			} else {
				String fileName = file.getOriginalFilename();
				String filepath = "C:/ruoyi/";
				File localFile = new File(filepath);
				if (!localFile.exists()) {
					localFile.mkdirs();
				}
				String path = FileUploadUtils.upload(Global.getAvatarPath(),
						file);
				GoodsAttach goodsAttach = new GoodsAttach();
				goodsAttach.setUrl(path);
				goodsAttach.setFileName(fileName);
				goodsAttach.setGoodsId(Integer.parseInt(goods_id));
				goodsAttachService.insertGoodsAttach(goodsAttach);				
			}
		}
		return AjaxResult.success("上传成功！");
	}
	
	/**
	 * 商品图片删除
	 */
	@PostMapping("/deleteSave")
	@ResponseBody
	public AjaxResult deleteSave(String key)
			throws IOException {
		Integer realKey=Integer.parseInt(key);
		GoodsAttach goodsAttach = goodsAttachService.selectGoodsAttachById(realKey);
		String fileName = goodsAttach.getUrl();	
		String Path =  "C:/" + fileName;
		File file = new File(Path);
		if (file.exists()) {
			FileUtils.deleteQuietly(file);
		}
		return toAjax(goodsAttachService.deleteGoodsAttachByIds(key));				
	}
	
	/**
	 * 修改保存商品
	 */
	@RequiresPermissions("Goods:goods:edit")
	@Log(title = "商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Goods goods) {
		GoodsAttach goodsAttach = new GoodsAttach();
		goodsAttach.setGoodsId(goods.getId());
		List<GoodsAttach> attachList = goodsAttachService
				.selectGoodsAttachList(goodsAttach);
		if (attachList.size() > 0) {
			GoodsAttach persist = attachList.get(0);
			goods.setGoodsTp(persist.getUrl());
		}
		return toAjax(goodsService.updateGoods(goods));
	}
	
	/**
	 * 删除商品
	 */
	@RequiresPermissions("Goods:goods:remove")
	@Log(title = "商品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(goodsService.deleteGoodsByIds(ids));
	}
	
	/**
	 * 商品审核 同意申请
	 */
	@PostMapping("/updateStatus")
	@ResponseBody
	public AjaxResult updateStatus(int id) {
		Goods persist=goodsService.selectGoodsById(id);
		persist.setGoodsLayout(APPConstants.CHUSHOUZHONG);
		persist.setValid(1);
		return toAjax(goodsService.updateGoods(persist));
	}
	
	/**
	 * 商品审核 同意申请
	 */
	@PostMapping("/updateStatus2")
	@ResponseBody
	public AjaxResult updateStatus2(int id) {
		Goods persist=goodsService.selectGoodsById(id);
		return AjaxResult.error("退回成功");
	}
	
}
